#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>//gethostname
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>//sockaddr_in
#include <string.h>//bzero
#include <netdb.h>//gethostbyname

#define BACKPORT 9000
#define MAXDATASIZE 100

int main(int argc,char *argv[])
{
	int sockfd;
	struct sockaddr_in addr;
	struct hostent *ht;
	char *host;
	char buf[MAXDATASIZE];
	int numberbytes;
	
	if(argc == 1)
	{
		char myname[255];
		gethostname(myname,255);
		host = myname;
	}
	else
	  host = argv[1];

	if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
	{
		perror("create socket");
		exit(EXIT_FAILURE);
	}

	if((ht = gethostbyname(host)) == NULL)
	{
		perror("gethostbyname");
		exit(EXIT_FAILURE);
	}
	
	addr.sin_family = AF_INET;
	addr.sin_port = htons(BACKPORT);
	addr.sin_addr = *((struct in_addr *)ht->h_addr);
	bzero(&(addr.sin_zero),8);
	

	if(connect(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr)) == -1)
	{
		perror("connect");
		exit(EXIT_FAILURE);
	}

	if((numberbytes = recv(sockfd,buf,MAXDATASIZE,0)) == -1)
	{
		perror("recv");
		exit(EXIT_FAILURE);
	}
	buf[numberbytes]='\0';
	printf("Received:%s\n",buf);
	close(sockfd);
	exit(EXIT_SUCCESS);
}
